// pages/xin-address/address.js
import {
  ajax,
  src,
  util
} from "../../utils/util.js";

var array = require('./city.js');
var objectMultiArray = array.objectMultiArray
var multiArray = array.multiArray
Page({

  /**
   * 页面的初始数据
   */
  data: {
    switch1Checked: true,
    // array: array1,
    // currentcityindex:0,
    item:"",
    selectbable:0,
    name:"",
    tel:"",
    detailadd: "",
    xinzeng:'',
    xuanzes:"",
    multiArray: multiArray,
    objectMultiArray: objectMultiArray,
    multiIndex: [0, 0],
  },
  switch1Change: function() {
    this.setData({switch1Checked:!this.data.switch1Checked})
  },

  bindMultiPickerChange: function (e) {
    var that = this
    that.setData({
      "multiIndex[0]": e.detail.value[0],
      "multiIndex[1]": e.detail.value[1]
    })
  },
  bindMultiPickerColumnChange: function (e) {
    var that=this
    // console.log(e.detail.value)
    switch (e.detail.column) {
      case 0:
        var list = []
        for (var i = 0; i < that.data.objectMultiArray.length; i++) {
          if (that.data.objectMultiArray[i].parid == that.data.objectMultiArray[e.detail.value].regid) {
            list.push(that.data.objectMultiArray[i].regname)
          }
        }
        that.setData({
          "multiArray[1]": list,
          "multiIndex[0]": e.detail.value,
          "multiIndex[1]": 0
        })

    }
  },



  xuanze: function() {
    // var currentcity = this.data.array[this.data.currentcityindex]
    var xinzeng = '新增'
    var xuanzes=this.data.xuanzes
    wx.redirectTo({
      url: '/pages/address-position/add-posi?currentcity=' + this.data.multiArray[1][this.data.multiIndex[1]] + '&xuanzes=' + xuanzes + '&xinzeng=' + xinzeng + '&multiIndex=' + this.data.multiIndex ,
    })
  },

  
  selectnow:function(e){
    var index = e.currentTarget.dataset.index;
    this.setData({
      selectbable:index
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#000',
    })
    
    if (options.xuanze) {
      this.setData({
        xuanzes: options.xuanze

        
      })
      wx.setStorageSync('xuanzes', options.xuanze)

    }
   
    if (options.item){
      var item = JSON.parse(options.item)
      this.setData({ item: item})
      this.setData({
        multiIndex: item.multiIndex.split(",")
      })

      var list = []
      for (var i = 0; i < this.data.objectMultiArray.length; i++) {
        if (this.data.objectMultiArray[i].parid == this.data.objectMultiArray[this.data.multiIndex[0]].regid) {
          list.push(this.data.objectMultiArray[i].regname)
        }
      }
      this.setData({
        "multiArray[1]": list,
      })
    }
  },

  getname:function(e){
    var name=e.detail.value
    this.setData({
      name: name
    })
  },
  gettel: function (e) {
    var tel = e.detail.value
    this.setData({
      tel: tel
    })
  },
  getdetailadd: function (e) {
    var detailadd = e.detail.value
    this.setData({
      detailadd: detailadd
    })
  },
  bindPickerChange:function(e){
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      currentcityindex: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  isPhoneNumber:function (tel) {
    var reg = /^0?1[3|4|5|6|7|8][0-9]\d{8}$/;
    return reg.test(tel);
  },

  baocun:function(){
    if (this.data.name==""){
      wx.showToast({
        title: '请输入姓名',
        icon: 'none',
        duration: 2000
      })
      return
    }
    if (this.data.item == "") {
      wx.showToast({
        title: '请选择地址',
        icon: 'none',
        duration: 2000
      })
      return
    }
    if (!this.isPhoneNumber(this.data.tel)) {
      wx.showToast({
        title: '请输入正确手机号码',
        icon: 'none',
        duration: 2000
      })
      return
    }
    if (this.data.detailadd == "") {
      wx.showToast({
        title: '请输入详细地址',
        icon: 'none',
        duration: 2000
      })
      return
    }
    var queue=[]
    var obj={
      ID:0,
      ConsigneeName:this.data.name,
      Phone:this.data.tel,
      ProvinceName: this.data.item.province,
      CityName: this.data.item.city,
      DistrictName: this.data.item.district,
      Locationaddress: this.data.item.address,
      Detailedaddress:this.data.detailadd,
      Label: this.data.selectbable,
      // Number
      // 标签 0家 1公司 2学校
      Longitude: this.data.item.location.lng,
      latitude: this.data.item.location.lat,
      Defaultaddress: this.data.switch1Checked?0:1
    }
    queue.push(obj)
    
    ajax('/api/Addaddress/ress?requestContent=' + JSON.stringify({
      token: wx.getStorageSync('Access_token'),
      queue: queue,

    }), {
        header: {
          'Accept': 'application/x-www-form-urlencoded', // 默认值
        },
        method: "post",
      }, res => {
        console.log(res)
        if (res.Message ="上传成功!"){
          wx.showToast({
            title: '上传成功',
            icon: 'none',
            duration: 2000
          })

          if (wx.getStorageSync('xuanzes') == '选择') {
            wx.redirectTo({
              url: '/pages/selectaddress/selectaddress',
            })
          } else if (wx.getStorageSync('xuanzes') == '管理') {
            wx.redirectTo({
              url: '/pages/adminaddress/adminaddress',
            })
          }

        }
      }
    )
  },
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})